﻿using System;                               //For delegate
using System.Collections.Generic;           //For Dictionary
using System.Timers;                        //For Timer
using System.Windows;
using System.Windows.Automation;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using System.Windows.Controls;
using LEET.CommonInterfaces;

namespace GUI.Custom_Classes
{
    /// <summary>
    /// Interaction logic for SI_Base.xaml
    /// </summary>
    public abstract class SI_Base : UserControl
    {
        AutomationPeer peer;
        public delegate void OnUpdate();
        protected Nil onUpdateDelegate = null;
        protected Dictionary<Timer, UIElement> TimerEvents;

        public SI_Base(Nil d)
        {
            onUpdateDelegate = d;
            TimerEvents = new Dictionary<Timer, UIElement>();
        }

        /// <summary>
        /// Returns a string representation of this command.
        /// </summary>
        public override string ToString()
        {
            string temp = base.ToString();
            if (temp.Contains("System.Windows.Controls.TextBox: "))
                temp = temp.Remove(0, "System.Windows.Controls.TextBox: ".Length);
            return temp;
        }

        
        /// <summary>
        /// Generates an AutomationEvent t
        /// </summary>
        /// <param name="newV"></param>
        protected void OnValueChanged(UIElement source, string newV)
        {
            peer = UIElementAutomationPeer.CreatePeerForElement(source);

            if (peer != null)
            {
                peer.RaisePropertyChangedEvent(
                    ValuePattern.ValueProperty,
                    "passkey",  //old value
                    newV);      //new value
            }
        }


        #region Delay Events

        protected void onChanged(object sender, TextChangedEventArgs e)
        {
            Start_Timer(sender as UIElement);
        }

        protected void onChanged(object sender, SelectionChangedEventArgs e)
        {
            Start_Timer(sender as UIElement);
        }

        private void OnTick(object sender, System.Timers.ElapsedEventArgs e)
        {
            Timer source = sender as Timer;
            source.Stop();

            if (TimerEvents[source] is TextBox)
            {
                TextBox element = TimerEvents[source] as TextBox;

                this.Dispatcher.Invoke(new System.Windows.Forms.MethodInvoker(delegate()
                {
                    OnValueChanged(element, element.Text);
                }));
            }
            
            onUpdateDelegate();
        }

        protected void Start_Timer(UIElement sender) 
        {
            foreach (Timer t in TimerEvents.Keys)
            {
                if (TimerEvents[t].Equals(sender))
                {
                    t.Stop();
                    t.Start();
                }
            }
        }

        protected void Setup_Events(UIElement source) 
        {
            if (TimerEvents.ContainsValue(source))
                return;

            Timer t = new Timer(2000);
            t.Elapsed += new System.Timers.ElapsedEventHandler(OnTick);

            TimerEvents.Add(t, source);
        }

        #endregion

        #region Automation

        protected override System.Windows.Automation.Peers.AutomationPeer OnCreateAutomationPeer()
        {
            return new SI_Base_AutomationPeer(this);
        }

        public class SI_Base_AutomationPeer : FrameworkElementAutomationPeer, IValueProvider
        {
            SI_Base host;

            public SI_Base_AutomationPeer(SI_Base rb)
                : base(rb)
            {
                host = rb;
            }

            #region FrameworkElementAutomationPeer

            public override Object GetPattern(PatternInterface patternInterface)
            {
                //if (patternInterface.Equals(System.Windows.Automation.Peers.PatternInterface.Value))
                //    return this;
                return base.GetPattern(patternInterface);
            }

            protected override string GetClassNameCore()
            {
                return host.GetType().Name;
            }

            protected override string GetAutomationIdCore()
            {
                return GetClassNameCore();
            }

            protected override string GetNameCore()
            {
                return host.Name;
            }

            protected override AutomationControlType GetAutomationControlTypeCore()
            {
                return AutomationControlType.Custom;
            }

            #endregion


            #region IValueProvider Members

            bool IValueProvider.IsReadOnly
            {
                get { return false; }
            }

            void IValueProvider.SetValue(string value)
            {
                
            }

            string IValueProvider.Value
            {
                get
                {
                    return host.ToString();
                }
            }

            #endregion
        }

        #endregion

    }
}
